#include "block.h"


block::block(IDirect3DDevice9 *d3ddev, FLOAT X, FLOAT Y, FLOAT Z, INT blockType)
{
	D3DCOLOR BlockColor;
	BlockColor = D3DCOLOR_ARGB(0xff, 255, 255, 255);


	CUSTOMVERTEX vertices[] =
	{
		{ X * 6 + -3.0f, Y * 6 + -3.0f, Z * 6 + -3.0f, BlockColor, 0.0f, 1.0f, },
		{ X * 6 + -3.0f, Y * 6 + 3.0f, Z * 6 + -3.0f, BlockColor, 0.0f, 0.0f, },
		{ X * 6 + 3.0f, Y * 6 + 3.0f, Z * 6 + -3.0f, BlockColor, 1.0f, 0.0f, },
		{ X * 6 + 3.0f, Y * 6 + -3.0f, Z * 6 + -3.0f, BlockColor, 1.0f, 1.0f, },
		{ X * 6 + -3.0f, Y * 6 + -3.0f, Z * 6 + 3.0f, BlockColor, 1.0f, 1.0f, },
		{ X * 6 + 3.0f, Y * 6 + -3.0f, Z * 6 + 3.0f, BlockColor, 1.0f, 0.0f, },
		{ X * 6 + 3.0f, Y * 6 + 3.0f, Z * 6 + 3.0f, BlockColor, 0.0f, 0.0f, },
		{ X * 6 + -3.0f, Y * 6 + 3.0f, Z * 6 + 3.0f, BlockColor, 0.0f, 1.0f, },
	};


	switch (blockType)
	{
	case 0:
		D3DXCreateTextureFromFile(d3ddev, "../img/dirt.png", &m_texture);
		break;
	case 1:
		D3DXCreateTextureFromFile(d3ddev, "../img/brick.png", &m_texture);
		break;
	case 2:
		D3DXCreateTextureFromFile(d3ddev, "../img/glass.png", &m_texture);
		break;
	case 3:
		D3DXCreateTextureFromFile(d3ddev, "../img/jack.png", &m_texture);
		break;
	case 4:
		D3DXCreateTextureFromFile(d3ddev, "../img/sand.png", &m_texture);
		break;
	case 5:
		D3DXCreateTextureFromFile(d3ddev, "../img/sponge.png", &m_texture);
		break;
	case 6:
		D3DXCreateTextureFromFile(d3ddev, "../img/stone.png", &m_texture);
		break;
	case 7:
		D3DXCreateTextureFromFile(d3ddev, "../img/tnt.png", &m_texture);
		break;
	case 8:
		D3DXCreateTextureFromFile(d3ddev, "../img/tree.png", &m_texture);
		break;
	case 9:
		D3DXCreateTextureFromFile(d3ddev, "../img/wood.png", &m_texture);
		break;
	default:
		D3DXCreateTextureFromFile(d3ddev, "../img/brick.jpg", &m_texture);
		break;
	}


	d3ddev->CreateVertexBuffer(sizeof(vertices),
		D3DUSAGE_WRITEONLY,
		CUSTOMFVF,
		D3DPOOL_MANAGED,
		&m_vbuffer,
		NULL);

	VOID* pVoid;

	m_vbuffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, vertices, sizeof(vertices));
	m_vbuffer->Unlock();

	short indices[] =
	{
		0, 1, 2,
		2, 3, 0,
		4, 5, 6,
		6, 7, 4,
		0, 3, 5,
		5, 4, 0,
		3, 2, 6,
		6, 5, 3,
		2, 1, 7,
		7, 6, 2,
		1, 0, 4,
		4, 7, 1
	};

	d3ddev->CreateIndexBuffer(sizeof(indices),
		D3DUSAGE_WRITEONLY,
		D3DFMT_INDEX16,
		D3DPOOL_MANAGED,
		&m_ibuffer,
		NULL);

	m_ibuffer->Lock(0, 0, (void**)&pVoid, 0);
	memcpy(pVoid, indices, sizeof(indices));
	m_ibuffer->Unlock();
}


block::~block()
{
}
